package cminus.graph;

import java.util.LinkedList;
import java.util.Queue;

import cminus.ast.ASTNode;

public class layer {
	
	public void setLayer(ASTNode node){
		int layerNum[]= new int[30];
		node.setLayer(0);
		Queue<ASTNode> q = new LinkedList<ASTNode>();
		q.add(node);
		while (!q.isEmpty()){
			ASTNode n = q.remove();
		
			int s=n.getNodeType();
			System.out.println(s);
		    switch (s){
			    case 1:n.setName("PROGRAM");break;
			    case 2:n.setName("STATEMENT_LIST");break;
			    case 3:n.setName("BLOCK");break;
			    case 4:n.setName("ASSIGNMENT_STATEMENT");break;
			    case 5:n.setName("EXPRESSION_STATEMENT");break;
			    case 6:n.setName("IF_STATEMENT");break;
			    case 7:n.setName("WHILE_STATEMENT");break;
			    case 8:n.setName("BREAK_STATEMENT");break;
			    case 9:n.setName("CONTINUE_STATEMENT");break;
			    case 10:n.setName("INPUT_STATEMENT");break;
			    case 11:n.setName("OUTPUT_STATEMENT");break;
			    case 12:n.setName("POS_EXPRESSION");break;
			    case 13:n.setName("NEG_EXPRESSION");break;
			    case 14:n.setName("NOT_EXPRESSION");break;
			    case 15:n.setName("ADD_EXPRESSION");break;
			    case 16:n.setName("SUB_EXPRESSION");break;
			    case 17:n.setName("MUL_EXPRESSION");break;
			    case 18:n.setName("LESS_EXPRESSION");break;
			    case 19:n.setName("LESS_EQUAL_EXPRESSION");break;
			    case 20:n.setName("GREATER_EXPRESSION");break;
			    case 21:n.setName("GREATER_EQUAL_EXPRESSION");break;
			    case 22:n.setName("EQUAL_EXPRESSION");break;
			    case 23:n.setName("NOT_EQUAL_EXPRESSION");break;
			    case 24:n.setName("AND_EXPRESSION");break;
			    case 25:n.setName("OR_EXPRESSION");break;
			    case 26:n.setName("IDENTIFIER");break;
			    case 27:n.setName("INTEGER_LITERAL");break;
			    default:n.setName("empty");
		    }
		
			for(int i=0;i<n.getChildrenNum();i++){
				
				 if(n.getChildIP(i)!=null){
					 		q.add(n.getChildIP(i));
							n.getChildIP(i).setLayer(n.getLayer()+1);
							
				 }
			}
		}
		
	}
	
	public int getMaxLayer(ASTNode node){
		int m=0;
		Queue<ASTNode> q = new LinkedList<ASTNode>();
		q.add(node);
		while (!q.isEmpty()){
			ASTNode n = q.remove();
			if(m<n.getLayer()) m=n.getLayer();
			for(int i=0;i<n.getChildrenNum();i++){
				
				 if(n.getChildIP(i)!=null){
					 		q.add(n.getChildIP(i));
							
				 }
			}
		}
		return m;
	}
	
	public void setLayerNum(ASTNode node){

		int layerNum[]= new int[30];
		
		Queue<ASTNode> q = new LinkedList<ASTNode>();
		q.add(node);
		while (!q.isEmpty()){
			
			ASTNode n = q.remove();
			int j = n.getLayer();
			layerNum[j]++;
		
			for(int i=0;i<n.getChildrenNum();i++){
				
				 if(n.getChildIP(i)!=null){
					 		q.add(n.getChildIP(i));
							
				 }
			}
		}
		q.add(node);
		while (!q.isEmpty()){
			ASTNode n = q.remove();
			int j = n.getLayer();
			n.setLayerNum(layerNum[j]);
		
			for(int i=0;i<n.getChildrenNum();i++){
				
				 if(n.getChildIP(i)!=null){
					 		q.add(n.getChildIP(i));
							
				 }
			}
		}
	}

}
